module DisplayHelper
  def top_click_pros(count=3)
    Product.find_by_sql("select a.id,a.name,b.name image_name,a.now_price from products a inner join pro_images b on a.id=b.product_id where img_type_id=1 order by click_count desc limit #{count}" )
  end
  def top_3_news
    Article.find(:all,:conditions=>["recommend=?",true],:order=>"created_at desc,click_count desc",:limit=>3,:select=>"id,title,image_name,short_description,article_category_id")
  end
  def top_news(count=15)
    Article.find(:all,:order=>"created_at desc",:limit=>count,:select=>"id,title,short_description,article_category_id,created_at")
  end

  def tejia_pros(count=8)
    Product.find_by_sql("select a.id,a.name,a.short_description,a.now_price,b.name image_name from products a
inner join pro_images b on a.id=b.product_id
inner join product_attribute_values c on c.product_id=a.id
inner join product_attribute_templates d on d.id=c.product_attribute_template_id
where b.img_type_id=1 and d.name='tejiakongtiao'
order by a.pro_order desc,a.click_count desc,a.created_at desc limit #{count}")

  end
  def tuijian_pros(count=8)
    Product.find_by_sql("select a.id,a.name,a.short_description,a.now_price,b.name image_name from products a
inner join pro_images b on a.id=b.product_id
inner join product_attribute_values c on c.product_id=a.id
inner join product_attribute_templates d on d.id=c.product_attribute_template_id
where b.img_type_id=1 and d.name='tuijiankongtiao'
order by a.pro_order desc,a.click_count desc,a.created_at desc limit #{count}")
    #.find_by_sql("select a.id,a.name,a.short_description,a.now_price,b.name image_name from products a inner join pro_images b on a.id=b.product_id inner join product_attribute_values c on c.product_id=a.id where b.img_type_id=1 and c.product_attribute_template_id=12 order by a.pro_order desc,a.click_count desc,a.created_at desc limit 8")

  end
  def get_top_tuangou_pros
    TuangouPro.all(:limit=>90,:order=>"click_count desc,created_at desc")
  end
  def jingpin_pros
    Product.find_by_sql("select a.id,a.name,a.short_description,a.now_price,b.name image_name from products a
inner join pro_images b on a.id=b.product_id
inner join product_attribute_values c on c.product_id=a.id
inner join product_attribute_templates d on d.id=c.product_attribute_template_id
where b.img_type_id=1 and d.name='jingpinkongtiao'
order by a.pro_order desc,a.click_count desc,a.created_at desc limit 8")

    # Product.find_by_sql("select a.id,a.name,a.short_description,a.now_price,b.name image_name from products a inner join pro_images b on a.id=b.product_id inner join product_attribute_values c on c.product_id=a.id where b.img_type_id=1 and c.product_attribute_template_id=9 order by a.pro_order desc,a.click_count desc,a.created_at desc limit 8")

  end
  def hotnews
    Article.find(:all,:order=>"click_count desc",:limit=>6,:select=>"id,title,article_category_id,image_name")

  end
  def cates
    ArticleCategory.all(:conditions=>"parent_id is not null",:order=>"cate_order")

  end
  def tuangou_top_click
    TuangouPro.all(:limit=>10,:order=>"click_count desc")

  end
  def tuangou_top_new
    TuangouPro.all(:limit=>10,:order=>"created_at desc")

  end
  #得到产品左侧导航,根据当前产品类别及导航标志符
  def get_list_items(names,procate_id=2)
    #ListItem.find_all_by_lookuplist_id(lookuplist_ids,:order=>"lookuplist_id")
    #ListItem.find_by_sql("select a.id,a.display_name,a.lookuplist_id,b.id template_id,c.id procate_id,b.data_type_id from list_items  where b.name in (#{names}) and c.id=#{procate_id}")
    ListItem.find(:all,:conditions=>["b.name in (?) and c.id=?",names,procate_id],
      :joins=>"a inner join product_attribute_templates b on b.lookuplist_id=a.lookuplist_id  inner join procates c on c.template_category_id=b.template_category_id",
      :order=>"a.ord,a.lookuplist_id",:select=>"a.id,a.display_name,a.lookuplist_id,b.id template_id,c.id procate_id,b.data_type_id,b.name,b.display_name template_name")
  end

    #得到列表
  def get_list_items_by_ids(ids)
    ListItem.find(:all,:conditions=>["lookuplist_id in (?)",ids])
  end
  #得到最新留言
  def get_newest_reviews
    Review.all(:order=>"created_at desc",:select=>"id,title,created_at",:limit=>10)
  end
  #根据产品id得到产品的属性,并按template_styles中的name进行分组
  def get_attributes_by_id(id)

    sql=%{
       select a.product_id,a.value_int,a.value_str,a.value_bool,a.value_bool,b.display_name,data_type_id,
        case
        when b.lookuplist_id>0 then (select display_name from list_items where list_items.lookuplist_id=b.lookuplist_id and list_items.id=a.value_int)
        else  ''
        end as lookuplistvalue,c.name,c.des
        from product_attribute_values a
        inner join
        product_attribute_templates b on a.product_attribute_template_id=b.id
        inner join template_styles c on c.id=b.template_style_id
        where product_id=%d  order by b.ord
    }%id
    ProductAttributeValue.find_by_sql(sql).group_by(&:name)
  end
#通过产品的属性对像，根据其数据类型来得到一个值对
  def get_value_by_item(item)
    case item.data_type_id
    when "1"#数据类型
      item.value_int
    when "2"#选择列表
      item.lookuplistvalue
    when "3"#字符类型
      item.value_str
    when "4"#布尔类型
      item.value_bool? ? "是":"否"
    end
  end

end
